package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.constant.RedisConstant;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * 管理端: 店铺状态控制器层
 * @author zgp
 * @version V1.0
 * @date 2025-01-19 9:34
 */
@RestController("adminShopController")
@RequestMapping("/admin/shop")
@Slf4j
@Api(tags = "店铺控制层")
public class ShopController {
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/status")
    @ApiOperation("获取店铺营业状态")
    public Result<Integer> getShopStatus(){
        Integer status = (Integer)redisTemplate.opsForValue().get(RedisConstant.SHOP_STATUS_KEY);
        log.info("获取店铺营业状态，status：{}", status == 1 ? "营业中" : "打烊中");
        return Result.success(status);
    }

    @PutMapping("/{status}")
    @ApiOperation("设置店铺营业状态")
    public Result<String> setShopStatus(@PathVariable Integer status){
        log.info("设置店铺营业状态，status：{}", status == 1 ? "营业中" : "打烊中");
        redisTemplate.opsForValue().set(RedisConstant.SHOP_STATUS_KEY, status, 1, TimeUnit.DAYS);
        return Result.success(MessageConstant.GENERAL_SUCCESS);
    }
}
